package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import com.itheima.health.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    //依赖注入
    @Reference
    private UserService userService;
    //依赖注入密码加密
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;
    /**
     * 根据用户名获取对应的菜单列表
     * @param username
     * @return
     */
    @RequestMapping("getMenuListByUsername")
    public Result getMenuListByUsername(String username) {
        try {
            List<Map<String, Object>> menuList = userService.getMenuListByUsername(username);
            return new Result(true, MessageConst.GET_MENU_SUCCESS, menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.GET_MENU_FAIL);
        }
    }




    /*
    删除会员信息
 */
    @RequestMapping("delete")
    public Result delete(Integer id){
        try {
            userService.delete(id);
            return new Result(true,MessageConst.DELETE_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.EDIT_MEMBER_FAIL);
        }
    }

    /*
       编辑会员信息
     */
    @RequestMapping("edit")
    public Result edit(@RequestBody User user){
        try {
            userService.edit(user);
            return new Result(true,MessageConst.EDIT_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.EDIT_MEMBER_FAIL);
        }
    }

    /*
        根据id查询会员
    */
    @RequestMapping("findById")
    public Result findById(Integer id){
        try {
            User user = userService.findById(id);
            return new Result(true,"查询会员信息成功",user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询会员信息失败");
        }
    }

    /*
       新增会员
     */
    @RequestMapping("add")
    public Result add(@RequestBody User user){
        try {
            //判断日期是否为空
            if(user.getBirthday()!=null){
                //Tue Feb 03 00:00:00 CST 1998  转换成自定义格式的日期
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                String userDate=sdf.format(user.getBirthday());
                user.setBirthday(DateUtils.parseString2Date(userDate));
            }
            //密码加密
            String password = passwordEncoder.encode(user.getPassword());
            user.setPassword(password);
            //调用service
            userService.add(user);
            return new Result(true,MessageConst.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ADD_MEMBER_FAIL);
        }
    }

    /*
       分页查询会员信息
     */
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        System.out.println(queryPageBean.getCurrentPage()+"==="+queryPageBean.getPageSize()+"==="+queryPageBean.getQueryString());
        //调用service,分页查询数据
        PageResult page = userService.findPage(queryPageBean);
        return page;

    }
    

    /**
     * 获取用户名
     * @return
     */
    @RequestMapping("/getUsername")
    public Result getUsername(){
        try {
            //从授权框架上下文对象中 获取认证对象
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            //再从 认证对象中 获取授权框架用户对象User
            org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) authentication.getPrincipal();
            //用户对象User 获取 用户名
            String username = user.getUsername();
            //获取成功, 返回成功信息, 并返回用户名
            return new Result(true, MessageConst.GET_USERNAME_SUCCESS, username);
        } catch (Exception e) {
            e.printStackTrace();
            //获取失败,返回失败信息
            return new Result(false, MessageConst.GET_USERNAME_FAIL);
        }
    }

    /**
     * 登录成功
     * @return
     */
    @RequestMapping("/loginSuccess")
    public Result loginSuccess(){
        return new Result(true,MessageConst.LOGIN_SUCCESS);
    }

    /**
     * 登录失败
     * @return
     */
    @RequestMapping("/loginFail")
    public Result loginFail(){
        return new Result(false,"登录失败");
    }


    @RequestMapping("/login")
    public Result login(String username, String password){
        System.out.println("oms backend====u:"+username+" ,p:"+password);
        if (userService.login(username, password)){
            System.out.println("login ok!!!");
            return new Result(true, MessageConst.ACTION_SUCCESS);
        } else {
            System.out.println("login fail");
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }
}
